package com.pink.redis;

/**
 * Redis配置类
 * * @author: 神月恭平
 * * @date: 2025/04/18 22:57
 */

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.RedisSerializer;

@Configuration
public class RedisConfig<T> {

    @Bean(name = "redisTemplate")
    public RedisTemplate<String,T> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String,T> template = new RedisTemplate<>(); // 创建RedisTemplate对象
        template.setConnectionFactory(factory); // 设置连接工厂
        template.setKeySerializer(RedisSerializer.string()); // 设置key序列化方式
        template.setValueSerializer(RedisSerializer.json()); // 设置value序列化方式
        template.setHashKeySerializer(RedisSerializer.string()); // 设置hashKey序列化方式
        template.setHashValueSerializer(RedisSerializer.json()); // 设置hashValue序列化方式
        template.afterPropertiesSet(); // 初始化RedisTemplate对象
        return template;
    }

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory){
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        return container;
    }
}
